﻿using System;
using System.Linq;
using Loki;
using Loki.Commands;
using Loki.UI;
using Luna.Model.Accounting;

namespace Luna.UI.Accounting
{
    public class AccountingCategoryListViewModel : LunaViewModel
    {
        public LokiCollection<DisplayNamedEntity> Categories { get; private set; }

        public AccountingCategoryListViewModel()
        {
            Categories = CreateTrackedCollection<DisplayNamedEntity>();

            Init += AccountingCategoryListViewModel_Init;
            Load += AccountingCategoryListViewModel_Load;
        }

        private void AccountingCategoryListViewModel_Load(object P_Sender, EventArgs e)
        {
            InitializeData();
        }

        private void AccountingCategoryListViewModel_Init(object P_Sender, EventArgs e)
        {
            Commands.Handle(ApplicationCommands.SAVE, Save_CanExecute, Save_Execute);
        }

        private void InitializeData()
        {
            Categories.Clear();
            foreach (IAccountingCategory L_Category in ApplicationModel.Accounting.Categories)
            {
                DisplayNamedEntity L_Display = new DisplayNamedEntity();
                L_Display.BeginInit();
                L_Display.ID = L_Category.ID;
                L_Display.Name = L_Category.Name;
                L_Display.Description = L_Category.Description;
                L_Display.EndInit();

                Categories.Add(L_Display);
            }
        }

        private void Save_Execute(object P_Sender, CommandEventArgs e)
        {
            RequiresValidation();
            if (IsValid)
            {
                try
                {
                    foreach (DisplayNamedEntity L_Display in Categories.RemovedItems)
                    {
                        IAccountingCategory L_Cat = ApplicationModel.Accounting.Categories.FirstOrDefault(x => x.ID == L_Display.ID);
                        if (L_Cat != null && ApplicationModel.Accounting.CanDelete(L_Cat))
                        {
                            ApplicationModel.Accounting.Delete(L_Cat);
                        }
                    }

                    foreach (DisplayNamedEntity L_Display in Categories.Where(x => x.IsDirty))
                    {
                        IAccountingCategory L_Cat = ApplicationModel.Accounting.Categories.FirstOrDefault(x => x.ID == L_Display.ID);
                        if (L_Cat == null)
                        {
                            L_Cat = ApplicationModel.Accounting.Create<IAccountingCategory>();
                        }

                        L_Cat.Name = L_Display.Name;
                        L_Cat.Description = L_Display.Description;

                        ApplicationModel.Accounting.Save(L_Cat);
                    }

                    InitializeData();
                    AcceptChanges();
                    UserMessage(Messages.UIMessages.SAVE_OK);
                }
                catch (Exception Ex)
                {
                    HandleException(Ex);
                }
            }
        }

        private void Save_CanExecute(object P_Sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= IsDirty;
        }

        protected override bool ValidateData(ValidationMode P_Mode)
        {
            bool L_Return = true;
            foreach (var item in Categories)
            {
                L_Return &= !string.IsNullOrWhiteSpace(item.Name);
                L_Return &= !string.IsNullOrWhiteSpace(item.Description);
            }

            return L_Return;
        }
    }
}
